<template>
  <div>
    <div class="category-title">
      <span>{{ category ? category.categoryName + "分类下的博客" : "分类" }}列表</span>
    </div>
    <div class="blog-list-box">
      <div class="blog-desc" @click="goto('/blog','BlogDetail',{id: item.id})" v-for="item in blogList" :key="item.id">
        <div class="blog-desc-left">
          <div v-if="item.picture" style="width: 100%;height: 100%">
            <img width="100%" height="100%" :src="item.picture" alt=""
                 :onerror="'this.src=' + '\'' + require('@/assets/image/me.jpg') + '\''">
          </div>
        </div>
        <div class="blog-desc-right">
          <div class="blog-title">
            <h2>{{ item.title }}</h2>
          </div>
          <div class="blog-description">
            <p>{{ item.description }}</p>
          </div>
          <div class="blog-base-info">
            <div class="blog-info-category">
              <el-link type="success">{{ item.category.categoryName }}</el-link>
            </div>
            <div class="blog-info-tag">
              <el-tag v-for="tag in item.tagList" :key="tag.id" effect="dark" size="mini" :color="getColor(tag.id)">
                {{ tag.tagName }}
              </el-tag>
            </div>
          </div>
          <div class="blog-issue-info">
            <div>
              <i class="iconfont icon-username"></i>&nbsp;&nbsp;
              <span>{{ item.user.nickname ? item.user.nickname : item.user.username }}</span>
            </div>
            <div>
              <i class="iconfont icon-date"></i>&nbsp;&nbsp;
              <span>{{ new Date(item.updateTime).format("yyyy年MM月dd日 hh:mm:ss") }}</span>
            </div>
            <div>
              <i class="iconfont icon-eyes"></i>&nbsp;&nbsp;
              <span>{{ item.views ? item.views : 0 }}</span>
            </div>
          </div>
        </div>
      </div>
      <transition name="el-zoom-in-top">
        <div class="msg-box" v-if="blogList.length == 0">
          <el-alert
            :closable="false"
            title="提示"
            type="info"
            description="该分类下暂无博客，换一个试试吧！"
            show-icon>
          </el-alert>
        </div>
      </transition>
      <transition name="el-collapse-transition">
        <div class="page-box" v-if="blogList.length != 0">
          <el-pagination
            style="margin-top: 20px;"
            background
            @size-change="sizeChange"
            @current-change="currentChange"
            :current-page="page.current"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
          </el-pagination>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  name: "CategoryBlogList",
  data() {
    return {
      blogList: [],
      page: {
        current: 1,
        size: 10,
        total: 0
      },
    }
  },
  computed: {
    category() {
      return this.$store.getters.getCategory;
    }
  },
  created() {
    this.getBlogList();
  },
  methods: {
    getColor(id) {
      return this.$Mine.getColor(id);
    },
    sizeChange(size) {
      this.page.size = size;
      this.getBlogList(this.curTagId);
    },
    currentChange(current) {
      this.page.current = current;
      this.getBlogList(this.curTagId);
    },
    getBlogList() {
      this.$get(this.$api.BLOG_LIST_URL, {
        categoryId: this.category.categoryId,
        current: this.page.current,
        size: this.page.size
      }).then(res => {
        if (res.data.code == 200) {
          this.blogList = res.data.data.records;
          this.page.total = res.data.data.total;
        }
      })
    },
    goto(path, name, params) {
      this.$router.push({
        path, name, params
      });
    },
  },
  watch: {
    category: {
      handler(newValue) {
        this.getBlogList();
      },
      deep: true
    }
  }
}
</script>

<style scoped>
.category-title {
  font-size: 24px;
  color: #9fc3c2;
  text-align: center;
  margin-bottom: 20px;
}

.blog-list-box > div p {
  margin: 0;
  padding: 5px 0 0 0;
  font-size: 14px;
  font-weight: normal;
  color: #a7cda4;
}

.blog-list-box {
  padding: 0 20px;
}

.blog-desc {
  display: flex;
  min-height: 100px;
  max-height: 180px;
  flex-direction: row;
  margin-top: 20px;
  border-radius: 15px;
  box-shadow: 0 0 6px 0px #a7cda4;
  overflow: hidden;
  cursor: pointer;
}

.blog-desc:hover {
  transform: translate(0, -10px);
  transition: .6s;
}

.blog-desc > .blog-desc-left {
  flex: 1;
}

.blog-desc > .blog-desc-right {
  flex: 4;
  display: flex;
  flex-direction: column;
  text-align: left;
  margin-left: 50px;
  box-sizing: content-box;
  position: relative;
}

.blog-desc > .blog-desc-right > .blog-title {
  display: flex;
  align-items: center;
  margin-top: 30px;
}

.blog-desc > .blog-desc-right > .blog-title h2 {
  font-size: 24px;
  font-weight: 500;
  padding: 0;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.blog-desc > .blog-desc-right p {
  font-size: 16px;
  font-weight: 300;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-all;
}

.blog-desc .blog-desc-right .blog-base-info {
  position: absolute;
  bottom: 30px;
  display: flex;
}

.blog-desc .blog-desc-right .blog-base-info .blog-info-tag .el-tag {
  margin-left: 10px;
  border: 0;
}

.blog-desc .blog-desc-right .blog-issue-info {
  position: absolute;
  bottom: 10px;
  display: flex;
  font-size: 12px;
  color: #838383;
}

.blog-desc .blog-desc-right .blog-issue-info i {
  font-size: 12px;
}

.blog-desc .blog-desc-right .blog-issue-info > div {
  margin-right: 30px;
}


</style>
